package router

import (
	"reflect"

	"gitee.com/actor168/gitserver/pkg/service"
	"github.com/gin-gonic/gin"
)

func Process(c *gin.Context) {
	// 处理请求逻辑
	req := RequestBody{}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}

	if req.Module != "gitRepository" {
		c.JSON(400, gin.H{"error": "非法模块名称" + req.Module})
		return
	}
	module := service.GitRepository
	if method, ok := reflect.TypeOf(module).MethodByName(req.Action); !ok {
		c.JSON(400, gin.H{"error": "非法方法名称" + req.Action})
		return
	} else {
		if method.Name != "GitOperate" {
			c.JSON(400, gin.H{"error": "非法方法名称" + req.Action})
			return
		}
		resp := module.GitOperate(&req.Params)
		c.JSON(200, gin.H{"data": resp})
	}

}

type RequestBody struct {
	Module    string              `json:"module"`
	Action    string              `json:"action"`
	Flag      string              `json:"flag"`
	BelongId  string              `json:"belongId"`
	Relations []any               `json:"relations"`
	Params    service.GitReqModel `json:"params"`
}
